import sympy as sp

# 定义符号变量和函数
x = sp.symbols('x')
f = x**3 - 3*x**2 + 2

# 计算导数
f_prime = sp.diff(f, x)
print("函数f(x)的导数是:", f_prime) # 函数f(x)的导数是: 3*x**2 - 6*x

# 求导数为零的点（临界点）
critical_points = sp.solve(f_prime, x)
print("临界点是:", critical_points) # 临界点是: [0, 2]

# 判断这些点是极大值还是极小值
for point in critical_points:
    # 计算二阶导数
    f_double_prime = sp.diff(f_prime, x)
    second_deriv_value = f_double_prime.subs(x, point)
    
    if second_deriv_value > 0:
        print(f"x={point}是局部极小值点") # x=0是局部极大值点
    elif second_deriv_value < 0:
        print(f"x={point}是局部极大值点") # x=2是局部极小值点
    else:
        print(f"x={point}可能是拐点")